import 'package:flutter/cupertino.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import './router/index.dart';
import './config/store_keys.dart';
import './utils/shared_preference.dart';

class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      localizationsDelegates: const [
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [Locale("zh", "CH")],
      locale: const Locale("zh"),
      onGenerateRoute: router.generator,
      home: CupertinoPageScaffold(
        child: Stack(
          children: const [RootPage()],
        ),
      ),
    );
  }
}

class RootPage extends StatefulWidget {
  const RootPage({super.key});

  @override
  State<RootPage> createState() => _HomeState();
}

class _HomeState extends State<RootPage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      String? username =
          await SharedStore.instance.getString(StoreKeys.username);
      bool configured = username != null && username.isNotEmpty;
      initPageJump(configured);
    });
  }

  void initPageJump(bool configured) {
    router.navigateTo(context, RouteNames.homepage, clearStack: true);
  }

  @override
  Widget build(BuildContext context) {
    return const Text("");
  }
}
